<template>
  <el-dialog :title="isEdit ? '编辑菜单信息' : '添加菜单信息'" :visible.sync="visible" :close-on-click-modal="false"
    :before-close="handleCloseDialog" width="600px">
    <el-form :model="dataObject" :rules="rules" ref="menuFrom" label-width="120px" size="mini">
      <el-form-item label="菜单名称：" prop="title">
        <el-input v-model="dataObject.title" placeholder="请输入菜单名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="上级菜单：">
        <treeselect v-model="dataObject.parentId" :options="selectMenuList" :normalizer="normalizer" :show-count="true"
          placeholder="选择上级菜单" />
      </el-form-item>
      <el-form-item label="菜单类型：" prop="type">
        <el-radio-group v-model="dataObject.type">
          <el-radio :label="0">菜单</el-radio>
          <el-radio :label="1">按钮</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="路由地址：" prop="name" v-if="dataObject.type === 0">
        <el-input v-model="dataObject.name" placeholder="请输入路由地址" clearable></el-input>
      </el-form-item>
      <el-form-item label="前端图标：" prop="icon" v-if="dataObject.type === 0">
        <el-input v-model="dataObject.icon" placeholder="请输入前端图标" clearable style="width: 90%"></el-input>
        <svg-icon style="margin-left: 8px" :icon-class="dataObject.icon"></svg-icon>
      </el-form-item>
      <el-form-item label="是否显示：">
        <el-radio-group v-model="dataObject.hidden">
          <el-radio :label="0">是</el-radio>
          <el-radio :label="1">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序：">
        <el-input v-model="dataObject.sort" placeholder="请输入排序" clearable></el-input>
      </el-form-item>
      <el-form-item> </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleConfirmDialog('menuFrom')" size="mini">提 交</el-button>
      <el-button @click="handleCloseDialog" size="mini">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { listAll, createMenu, updateMenu, getMenu } from "@/api/menu";
import { createNamespacedHelpers } from "vuex";
import { handleTree } from '@/utils/tree';
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

const { mapState } = createNamespacedHelpers("umsMenu");

const defaultObject = {
  title: "",
  parentId: 0,
  name: "",
  type: 0,
  icon: "",
  hidden: 0,
  sort: 0
};

export default {
  name: "editObjDialog",
  components: { Treeselect },
  data() {
    return {
      dataObject: Object.assign({}, defaultObject),
      selectMenuList: []
    };
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    dataId: {
      type: Number,
      default: null
    }
  },
  computed: {
    ...mapState({
      rules: state => state.rules
    })
  },
  mounted() {
    if (this.isEdit) {
      getMenu(this.dataId).then(response => {
        this.dataObject = response.data;
      });
    } else {
      this.dataObject = Object.assign({}, defaultObject);
    }
    this.getTreeselect();
  },
  methods: {
    handleCloseDialog() {
      this.$emit("update:visible");
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      listAll().then(response => {
        this.selectMenuList = [];
        const menu = { id: 0, title: '主类目', children: [] };
        menu.children = handleTree(response.data, "id");
        this.selectMenuList.push(menu);
        if (!this.isEdit) {
          if (this.dataId) {
            this.dataObject.parentId = this.dataId;
          } else {
            this.dataObject.parentId = 0;
          }
        }
      });
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.title,
        children: node.children
      };
    },
    handleConfirmDialog(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$confirm("是否提交数据", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            if (this.isEdit) {
              updateMenu(this.dataId, this.dataObject).then(response => {
                this.resetForm(formName);
                this.$message({
                  message: "修改成功",
                  type: "success",
                  duration: 1000
                });
                this.$emit("handle-confirm");
              });
            } else {
              createMenu(this.dataObject).then(response => {
                this.resetForm(formName);
                this.$message({
                  message: "提交成功",
                  type: "success",
                  duration: 1000
                });
                this.$emit("handle-confirm");
              });
            }
          });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dataObject = Object.assign({}, defaultObject);
    }
  }
};
</script>
